package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.Device_InDao;
import com.zretc.device.entity.Device_In;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import javax.swing.DefaultComboBoxModel;

public class Device_InFindDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField txtin_id;
	private JTextField txtdevice_code;
	private JTextField txtin_date;
	private JTextField txtprovider;
	private JTextField txtprovider_tele;
	private JTextField txtin_num;
	private JTextField txtprice;
	private JTextField txtbuyer;
	private JTable table;
	private String tin_id;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_InFindDialog dialog = new Device_InFindDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_InFindDialog() {
		setBounds(-18, -47, 942, 623);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JComboBox CBoption = new JComboBox();
		CBoption.setModel(new DefaultComboBoxModel(new String[] { "入库单号", "设备编号", "入库日期" }));
		CBoption.setFont(new Font("宋体", Font.PLAIN, 20));
		CBoption.setBounds(220, 57, 329, 30);
		contentPanel.add(CBoption);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		scrollPane.setBounds(20, 231, 806, 284);
		contentPanel.add(scrollPane);
		{
			JLabel label = new JLabel("请选择查询方式");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(20, 51, 186, 38);
			contentPanel.add(label);
		}
		{
			JLabel label = new JLabel("请输入查询信息");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(20, 113, 186, 30);
			contentPanel.add(label);
		}

		{
			textField = new JTextField();
			textField.setFont(new Font("宋体", Font.PLAIN, 25));
			textField.setColumns(10);
			textField.setBounds(220, 113, 329, 30);
			contentPanel.add(textField);
		}
		{
			JButton button = new JButton("查询");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 获取复选框里的值
					String opString = CBoption.getSelectedItem() + "";
					// 获取文本框里的值
					String txt = textField.getText();
					if (txt.contentEquals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						if (opString.equals("入库单号")) {
							// 调用方法
							int inId = Integer.parseInt(txt);
							Device_In di = fillTableById(scrollPane, inId);
							if (di == null) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该单号不存在");
							}
						} else if (opString.equals("设备编号")) {
							// 调用方法
							List<Device_In> list = fillTableByCode(scrollPane, txt);
							if (list.isEmpty()) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该设备编号不存在");
							}
						}

						else if (opString.equals("入库日期")) {
							// 调用方法
							List<Device_In> list = fillTableByDate(scrollPane, txt);
							if (list.isEmpty()) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该入库日期不存在");
							}
						}
					}
				}
			});
			button.setFont(new Font("宋体", Font.PLAIN, 20));
			button.setBounds(575, 115, 113, 30);
			contentPanel.add(button);
		}
		{
			JButton button = new JButton("取消");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					textField.setText("");
					fillTable(scrollPane);
				}
			});
			button.setFont(new Font("宋体", Font.PLAIN, 20));
			button.setBounds(713, 115, 113, 30);
			contentPanel.add(button);
		}
		{
			txtin_id = new JTextField();
			txtin_id.setEditable(false);
			txtin_id.setColumns(10);
			txtin_id.setBounds(20, 173, 86, 30);
			contentPanel.add(txtin_id);
		}
		{
			txtdevice_code = new JTextField();
			txtdevice_code.setColumns(10);
			txtdevice_code.setBounds(106, 173, 86, 30);
			contentPanel.add(txtdevice_code);
		}
		{
			txtin_date = new JTextField();
			txtin_date.setColumns(10);
			txtin_date.setBounds(191, 173, 86, 30);
			contentPanel.add(txtin_date);
		}
		{
			txtprovider = new JTextField();
			txtprovider.setColumns(10);
			txtprovider.setBounds(276, 173, 86, 30);
			contentPanel.add(txtprovider);
		}
		{
			txtprovider_tele = new JTextField();
			txtprovider_tele.setColumns(10);
			txtprovider_tele.setBounds(362, 173, 113, 30);
			contentPanel.add(txtprovider_tele);
		}
		{
			txtin_num = new JTextField();
			txtin_num.setColumns(10);
			txtin_num.setBounds(475, 173, 86, 30);
			contentPanel.add(txtin_num);
		}
		{
			txtprice = new JTextField();
			txtprice.setColumns(10);
			txtprice.setBounds(560, 173, 86, 30);
			contentPanel.add(txtprice);
		}
		{
			txtbuyer = new JTextField();
			txtbuyer.setColumns(10);
			txtbuyer.setBounds(644, 173, 86, 30);
			contentPanel.add(txtbuyer);
		}
		{
			JButton button = new JButton("确认修改");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 获取文本框里的值
					String tin_id = txtin_id.getText();
					String device_code = txtdevice_code.getText();
					String in_date = txtin_date.getText();
					String provider = txtprovider.getText();
					String tprovider_tele = txtprovider_tele.getText();
					String tin_num = txtin_num.getText();
					String tprice = txtprice.getText();
					String buyer = txtbuyer.getText();
					// 判断输入的值
					if (tin_id.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "入库单号不能为空");
					} else if (device_code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "设备编号不能为空");
					} else if (in_date.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "入库日期不能为空");
					} else if (provider.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "供应商不能为空");
					} else if (tprovider_tele.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "供应商电话不能为空");
					} else if (tprice.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "设备单价不能为空");
					} else if (buyer.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "采购员姓名不能为空");
					} else {
						if (tprovider_tele.matches("^((13[0-9])|(15[^4])|(17[^9])|(18[^14]))\\d{8}$")) {
							// 转型
							int in_id = Integer.parseInt(tin_id);
							int in_num = Integer.parseInt(tin_num);
							Double price = Double.parseDouble(tprice);
							// 创建对象
							Device_In di = new Device_In(in_id, device_code, in_date, provider, tprovider_tele, in_num,
									price, buyer);
							Device_InDao did = new Device_InDao();
							boolean flag = did.modDevIn(di);
							if (flag) {
								JOptionPane.showMessageDialog(null, "修改成功");
								// 更新
								fillTable(scrollPane);
							} else {
								JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
							}
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "供应商电话不是一个正确的号码");
						}
					}
				}
			});
			button.setFont(new Font("宋体", Font.PLAIN, 20));
			button.setBounds(744, 173, 119, 27);
			contentPanel.add(button);
		}

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				// 取值
				tin_id = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String device_code = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String in_date = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				// 供应商
				String provider = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				// 供应商电话
				String tprovider_tele = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String tin_num = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
				String tprice = table.getValueAt(table.rowAtPoint(e.getPoint()), 6) + "";
				String buyer = table.getValueAt(table.rowAtPoint(e.getPoint()), 7) + "";
				// 给文本框赋值
				txtin_id.setText(tin_id);
				txtdevice_code.setText(device_code);
				txtin_date.setText(in_date);
				txtprovider.setText(provider);
				txtprovider_tele.setText(tprovider_tele);
				txtin_num.setText(tin_num);
				txtprice.setText(tprice);
				txtbuyer.setText(buyer);
			}
		});
		table.setRowHeight(25);
		{
			popupMenu = new JPopupMenu();
			addPopup(table, popupMenu);
			{
				mntmNewMenuItem = new JMenuItem("删除");
				mntmNewMenuItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 让用户选择
						int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！",
								"删除入库单号为: " + tin_id + " 的所有信息", JOptionPane.YES_NO_OPTION);
						if (n == 0) {
							// 转型
							int in_id = Integer.parseInt(tin_id);
							// 获取编号
							Device_InDao did = new Device_InDao();
							boolean flag = did.delDevIn(in_id);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "删除成功");
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
							}
							// 更新
							fillTable(scrollPane);
						}
					}
				});
				popupMenu.add(mntmNewMenuItem);
			}
		}
		fillTable(scrollPane);
		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * 查所有
	 * 
	 * @param scrollPane
	 */
	private void fillTable(JScrollPane scrollPane) {
		// 创建对象
		Device_InDao did = new Device_InDao();
		// 调用查询所有的方法，并接收返回值
		List<Device_In> list = did.findDevInAll();
		int row = list.size();
		// 创建二维数组
		Object[][] obj = new Object[row][8];
		// 循环遍历
		for (int i = 0; i < obj.length; i++) {
			Device_In di = list.get(i);
			// 赋值
			obj[i][0] = di.getIn_id();
			obj[i][1] = di.getDevice_code();
			obj[i][2] = di.getIn_date();
			obj[i][3] = di.getProvider();
			obj[i][4] = di.getProvider_tele();
			obj[i][5] = di.getIn_num();
			obj[i][6] = di.getPrice();
			obj[i][7] = di.getBuyer();
		}

		table.setModel(new DefaultTableModel(obj,
				new String[] { "\u5165\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5165\u5E93\u65E5\u671F",
						"\u4F9B\u5E94\u5546", "\u4F9B\u5E94\u5546\u7535\u8BDD", "\u5165\u5E93\u6570\u91CF",
						"\u4EF7\u683C", "\u91C7\u8D2D\u8005" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 根据设备编号查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_In> fillTableByCode(JScrollPane scrollPane, String devCode) {
		// 创建对象
		Device_InDao did = new Device_InDao();
		// 调用查询所有的方法，并接收返回值
		List<Device_In> list = did.findDevInByCode(devCode);
		if (list != null) {
			int row = list.size();
			// 创建二维数组
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_In di = list.get(i);
				// 赋值
				obj[i][0] = di.getIn_id();
				obj[i][1] = di.getDevice_code();
				obj[i][2] = di.getIn_date();
				obj[i][3] = di.getProvider();
				obj[i][4] = di.getProvider_tele();
				obj[i][5] = di.getIn_num();
				obj[i][6] = di.getPrice();
				obj[i][7] = di.getBuyer();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u5165\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5165\u5E93\u65E5\u671F",
							"\u4F9B\u5E94\u5546", "\u4F9B\u5E94\u5546\u7535\u8BDD", "\u5165\u5E93\u6570\u91CF",
							"\u4EF7\u683C", "\u91C7\u8D2D\u8005" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	/**
	 * 根据入库日期查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_In> fillTableByDate(JScrollPane scrollPane, String inDate) {
		// 创建对象
		Device_InDao did = new Device_InDao();
		// 调用查询所有的方法，并接收返回值
		List<Device_In> list = did.findDevInByDate(inDate);
		if (list != null) {
			int row = list.size();
			// 创建二维数组
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_In di = list.get(i);
				// 赋值
				obj[i][0] = di.getIn_id();
				obj[i][1] = di.getDevice_code();
				obj[i][2] = di.getIn_date();
				obj[i][3] = di.getProvider();
				obj[i][4] = di.getProvider_tele();
				obj[i][5] = di.getIn_num();
				obj[i][6] = di.getPrice();
				obj[i][7] = di.getBuyer();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u5165\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5165\u5E93\u65E5\u671F",
							"\u4F9B\u5E94\u5546", "\u4F9B\u5E94\u5546\u7535\u8BDD", "\u5165\u5E93\u6570\u91CF",
							"\u4EF7\u683C", "\u91C7\u8D2D\u8005" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	/**
	 * 根据单号查询
	 * 
	 * @param scrollPane
	 */
	private Device_In fillTableById(JScrollPane scrollPane, int inId) {
		// 创建对象
		Device_InDao did = new Device_InDao();
		// 根据单号查询，并接收返回值
		Device_In di = did.findDevInById(inId);
		if (di != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][8];
			// 赋值
			obj[0][0] = di.getIn_id();
			obj[0][1] = di.getDevice_code();
			obj[0][2] = di.getIn_date();
			obj[0][3] = di.getProvider();
			obj[0][4] = di.getProvider_tele();
			obj[0][5] = di.getIn_num();
			obj[0][6] = di.getPrice();
			obj[0][7] = di.getBuyer();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u5165\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5165\u5E93\u65E5\u671F",
							"\u4F9B\u5E94\u5546", "\u4F9B\u5E94\u5546\u7535\u8BDD", "\u5165\u5E93\u6570\u91CF",
							"\u4EF7\u683C", "\u91C7\u8D2D\u8005" }));
			scrollPane.setViewportView(table);
		}
		return di;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
